package com.itHaipeng.web.customachieve;
import com.alibaba.fastjson.JSON;
import com.itHaipeng.factory.IServiceImplFactory;
import com.itHaipeng.object.Brand;
import com.itHaipeng.service.IService;
import com.itHaipeng.utils.PageBean;
import com.itHaipeng.web.Custom;
import org.jetbrains.annotations.NotNull;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.IOException;
import java.util.List;
import java.util.Objects;

@WebServlet("/com/*")
public class MethodServlet extends Custom {
    private static final  IService iServiceImpl = IServiceImplFactory.getIServiceImpl();
    public void selectAll(@NotNull HttpServletRequest request, @NotNull HttpServletResponse response) throws IOException {
        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/json;charset=UTF-8");
        List<Brand> brands = iServiceImpl.selectAll();
        //转换为JSON对象
        if(brands.toString()!=null){
            String toJSONString = JSON.toJSONString(brands);
            response.getWriter().write(toJSONString);
        }else {
            response.getWriter().write("失败");
        }
    }
    public void insert(@NotNull HttpServletRequest request, @NotNull HttpServletResponse response) throws IOException{
        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/json;charset=UTF-8");
        //获取前端传来的字符串
        BufferedReader requestReader = request.getReader();
        String s = requestReader.readLine();
        //转换为JSON对象
        Brand brand = JSON.parseObject(s, Brand.class);
        if(brand.toString()==null){
            response.getWriter().write("fail");
        }else {
            iServiceImpl.insert(brand);
            response.getWriter().write("success");
        }
    }
    public void deleteByIds(@NotNull HttpServletRequest request , @NotNull HttpServletResponse response)throws IOException{
        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/json;charset=UTF-8");
        BufferedReader requestReader = request.getReader();
        String s = requestReader.readLine();
        if(s!=null){
            int[] ids = JSON.parseObject(s, int[].class);
            iServiceImpl.deleteByIds(ids);
            response.getWriter().write("success");
        }else {
            response.getWriter().write("fail");
        }
    }
    public void selectByPage(@NotNull HttpServletRequest request, @NotNull HttpServletResponse response)throws IOException{
        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/json;charset=UTF-8");
        int page = Integer.parseInt(request.getParameter("page"));
        int  size = Integer.parseInt(request.getParameter("size"));
        if(page!=0&&size!=0){
            int begin=(page-1)*size;
            PageBean<Brand> pageBean = iServiceImpl.selectByPage(begin, size);
            //转json对象
            String pageBeanString = JSON.toJSONString(pageBean);
            response.getWriter().write(pageBeanString);
        }else {
            response.getWriter().write("失败");
        }
    }
    public void selectByPageCondition(HttpServletRequest request,HttpServletResponse response)throws IOException{
        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/json;charset=UTF-8");
        String page = request.getParameter("page");
        String size = request.getParameter("size");
        int _page=Integer.parseInt(page);
        int _size=Integer.parseInt(size);
        int begin=(_page-1)*_size;
        System.err.println(begin);
        //获取前台得到的brand对象
        BufferedReader reader = request.getReader();
        String s = reader.readLine();
        System.err.println(s);
        int i=0;
            Brand brand = JSON.parseObject(s, Brand.class);
        System.err.println(brand.toString());
            brand.setBrandName("%"+brand.getBrandName()+"%");
            brand.setCompanyName("%"+brand.getCompanyName()+"%");
            PageBean<Brand> pageBean = iServiceImpl.selectByPageCondition(begin,_size,brand);
            if(pageBean.getRows().toString()!=null&&pageBean.getTotalCount()!=0) {
                System.err.println(pageBean.toString());
                response.getWriter().write(JSON.toJSONString(pageBean));
            }else {
                response.getWriter().write("失败");
            }
    }
     public void upData(HttpServletRequest request,HttpServletResponse response)throws IOException{
        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/json;charset=UTF-8");
        //获取json
        BufferedReader reader = request.getReader();
        String s = reader.readLine();
        if(s!=null){
            Brand brand = JSON.parseObject(s, Brand.class);
            iServiceImpl.upData(brand);
            response.getWriter().write("success");
        }else {
            response.getWriter().write("Fail");
        }
    }
}
